{% extends "layout.html" %}

{% block title %}任务定制{% endblock %}

{% block main_content %}
    <!-- MAIN CONTENT -->
    <div class="main-content">
        <div class="main-content-inner">
            <div class="page-content">
                <div class="page-header">
                    <h1>
                        Job Difine
                    </h1>
                </div><!-- /.page-header -->
                <div class="clearfix">
                    <div class="pull-right tableTools-container">
                        <div class="dt-buttons btn-overlap btn-group">
                            <a id="new_job_btn"
                               class="dt-button buttons-copy buttons-html5 btn btn-white btn-primary btn-bold"
                               data-rel=tooltip tabindex="0" aria-controls="dynamic-table" data-original-title=""
                               title="New">
                                <span>
                                    <i class="fa glyphicon-plus bigger-110 pink"></i>
                                </span>
                            </a>
                        </div>
                    </div>
                </div>
                <div class="table-header">

                </div>
                <div>
                    <div class="table-responsive">
                        <table id="job_define" class="table table-striped table-bordered table-hover">
                        </table>
                    </div>
                </div>
            </div><!-- /.page-content -->
        </div>
    </div><!-- /.main-content -->
    {% include "job_define_edit_modal.html" %}
    {% include "job_define_json_view.html" %}
{% endblock %}

{% block script_extend %}
    <!-- END MAIN CONTENT -->
    <script type="text/javascript">
        $('[data-rel=tooltip]').tooltip();
        var mData = [
            {
                "mData": "name",
                "bSortable": false,
                "sTitle": "标识"
            }, {
                "mData": "displayName",
                "bSortable": false,
                "sTitle": "描述"
            }, {
                "mData": "defJson",
                "bSortable": false,
                "sTitle": "JobJson",
                "mRender": function (data, type, row) {
                    return '<a class="btn btn-link" data-tb-primary="' + row.name + '" data-callback="reloadTable()" data-ajax-json-view="/job_define/getJobJsonViewer">点击查看</a>';
                }
            }, {
                "mData": "isIncrement",
                "bSortable": false,
                "sTitle": "是否增量",
                "mRender": function (data, type, row) {
                    if (data === true) {
                        return '<span class="label label-info label-white">增量</span>';
                    } else {
                        return '<span class="label label-danger label-white">非增量</span>';
                    }
                }
            }, {
                "mData": "incrementParam",
                "bSortable": false,
                "sTitle": "增量参数",
                "defaultContent": "未设置"
            }, {
                "mData": "incrementParamStart",
                "bSortable": false,
                "sTitle": "增量起始",
                "defaultContent": "未设置"
            }, {
                "mData": "jobCron",
                "bSortable": false,
                "sTitle": "jobCron",
                "defaultContent": "未设置"
            }, {
                "mData": "isRun",
                "bSortable": false,
                "sTitle": "运行状态",
                "mRender": function (data, type, row) {
                    if (data === true) {
                        return '<span class="label label-success label-white">运行中</span>';
                    } else {
                        return '<span class="label label-danger label-white">已停止</span>';
                    }
                }
            }, {
                "mData": null,
                "bSortable": false,
                "sTitle": "操作",
                "mRender": function (data, type, row) {
                    var r = [];
                    r.push('<a href="javascript:void(0);" class="btn btn-link" data-tb-primary="' + row.name + '" data-callback="reloadTable()" data-ajax-modal="/job_define/getEditForm');
                    r.push('">编辑</a> ');
                    if (row.isRun === true) {
                        r.push('<a href="javascript:void(0);" class="btn btn-link" data-callback="reloadTable()" data-confirm="您确定需要停止【' + row.name + '】这个任务吗?" data-ajax-service="/job_instance/stop?name=');
                        r.push(row.name);
                        r.push('">停止</a>');
                    } else {
                        r.push('<a href="javascript:void(0);" class="btn btn-link" data-callback="reloadTable()" data-confirm="您确定需要启动【' + row.name + '】这个任务吗?" data-ajax-service="/job_instance/startup?name=');
                        r.push(row.name);
                        r.push('">启动</a>');
                    }
                    return r.join("");
                }
            }];


        /* 绘制table */
        var initTable = function (url) {
            return initDataTable("job_define", {
                "aoColumns": mData,
                "bDestroy": true,
                "fnDrawCallback": function () {

                }
            }, url, true);
        };

        var dataTable = initTable("/job_define/list");

        /* 刷新table */
        function reloadTable() {
            dataTable.fnReloadAjax();
        }


        $(document).on('click.modal.api', '[data-ajax-modal]', function () {
            var $this = $(this);
            var href = $this.attr("data-ajax-modal");
            var primary = $this.attr("data-tb-primary");
            var callback = $this.attr('data-callback');

            Action.jsonAsyncActByData("/job_define/get", {"name": primary}, function (data) {
                if (data.level == 'success' && data.data) {
                    var jobDef = data.data;
                    $("#name").val(jobDef.name);
                    $("#displayName").val(jobDef.displayName);
                    $("#defJson").val(jobDef.defJson);
                    $("#isIncrement").val(jobDef.isIncrement === true ? 1 : 0);
                    $("#incrementParam").val(jobDef.incrementParam);
                    $("#incrementParamStart").val(jobDef.incrementParamStart);
                    $("#jobCron").val(jobDef.jobCron);
                    $("#edit_modal").modal({
                        keyboard: false,
                        show: true,
                        backdrop: 'static'
                    });

                    $("#edit_modal").find(".modal-footer .btn-primary").on("click", function () {
                        var form = $("#edit_form");
                        Action.jsonAsyncActByData("/job_define/modify", form.serialize(), function (data) {
                            if (data.level == 'danger') {
                                showTopAlert(data.level, data.title, data.msg);
                            } else {
                                $('#edit_modal').on('hide.bs.modal', function (e) {
                                    $(form)[0].reset();
                                });;
                                $("#edit_modal").modal('hide');
                                showTopAlert(data.level, data.title, data.msg);
                                reloadTable();
                            }
                        });
                    });

                } else {
                    showTopAlert(data.level, data.title, data.msg);
                }
            });

        });

        $("#new_job_btn").on("click", function () {
            $("#edit_modal").modal({
                keyboard: false,
                show: true,
                backdrop: 'static'
            });

            $("#edit_modal").find(".modal-footer .btn-primary").on("click", function () {
                var form = $("#edit_form");
                Action.jsonAsyncActByData("/job_define/create", form.serialize(), function (data) {
                    if (data.level == 'danger') {
                        showTopAlert(data.level, data.title, data.msg);
                    } else {
                        $('#edit_modal').on('hide.bs.modal', function (e) {
                            $(form)[0].reset();
                        });
                        $("#edit_modal").modal('hide');
                        showTopAlert(data.level, data.title, data.msg);
                        reloadTable();
                    }
                });
            });
        });

        $(document).on('click.modal.api', '[data-ajax-json-view]', function () {
            var $this = $(this);
            var primary = $this.attr("data-tb-primary");
            Action.jsonAsyncActByData("/job_define/get", {"name": primary}, function (data) {
                if (data.level == 'success' && data.data) {
                    var jobDef = data.data;

                    $("#json_content").JSONView(jobDef.defJson);

                    $("#json_view_modal").modal({
                        keyboard: false,
                        show: true
                    });

                } else {
                    showTopAlert(data.level, data.title, data.msg);
                }
            });

        });
    </script>
{% endblock %}